<template>
  <div style="overflow-y: auto;  height: 100%;">
    <el-form ref="elForm" :model="formData" :rules="rules" size="medium" label-width="80px" style="overflow: hidden">
      <el-form-item label="文件名称" prop="title">
        <el-input v-model="formData.title" placeholder="请输入文件名称" clearable :style="{width: 'calc(100% - 95px)'}"
                  :disabled="disabled"></el-input>
        <el-upload
          v-if="!disabled"
          class="upload-demo"
          :action="mainAction+'?type='+type_id"
          :on-success="mainUploadSuccess"
          :on-progress="uploadProgress"
          style="display: inline-block; margin-left: 10px"
          :show-file-list="false">
          <el-button size="small" type="primary">上传正文</el-button>
        </el-upload>
        <span style="font-size: 12px; color: #de4c4a">注：严禁上传涉密文件（限50M以内文件）</span>
      </el-form-item>

      <el-form-item label="是否公开" prop="is_open">
        <el-radio-group v-model="formData.is_open">
          <el-radio :label="false">否</el-radio>
          <el-radio :label="true">是</el-radio>
        </el-radio-group>
      </el-form-item>

      <el-form-item label="拟稿单位" prop="dept_id">
        <dept-select-tree v-model="formData.dept_id" placeholder="请选择拟稿单位" dept-type="my"></dept-select-tree>
      </el-form-item>
      <el-form-item label="拟稿人" prop="alias">
        <el-input v-model="formData.alias" placeholder="请输入合同名称" clearable :style="{width: '100%'}"
                  :disabled="true"></el-input>
      </el-form-item>
      <el-form-item label="电话" prop="phone">
        <el-input v-model="formData.phone" placeholder="请输入手机号/座机" clearable :style="{width: '100%'}"></el-input>
      </el-form-item>
      <el-form-item label="上传附件" prop="files">
        <my-upload-file desc="若上传EXCEL文件，请先调整打印模式以便后续预览" v-model="formData.files">
        </my-upload-file>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
  import Treeselect from "@riophae/vue-treeselect";
  import "@riophae/vue-treeselect/dist/vue-treeselect.css";
  import schoolDocuments from "@/api/officialDocument/schoolDocuments/index"
  import schoolSignature from "@/api/officialDocument/schoolSignature/index"
  import eventBus from "@/utils/eventBus";
  import user from "@/api/systemManagement/user/index";
  import MyUploadFile from "../../../components/Common/MyUploadFile";
  import DeptSelectTree from "@/components/Common/DeptSelectTree";
  import {mapGetters} from "vuex";

  export default {
    name: "LeftFormSignature",
    components: {DeptSelectTree, MyUploadFile, Treeselect},
    props: ["row", "disabled"],
    data() {
      return {
        formData: {
          title: undefined,
          files: null,
          is_draft: 0,
          dept_id: undefined,
          alias: "",
          text_file: "",
          type_id: 3,//公文类型 1.校内签呈 2.学校收文 3.学校发文,4.部门发文,
          is_edit: false,
          phone: undefined,
          is_edit: false,
          is_open: false,
        },
        rules: {
          title: [{
            required: true,
            message: '请输入文件名称',
            trigger: 'blur'
          }],
          dept_id: [{
            required: true,
            message: '请选择拟稿单位',
            trigger: 'blur'
          }],
          phone: [
            {required: true, message: '请输入手机号/座机'},
            {
              validator: this.checkMyPhone,
              message: '请输入正确的手机号/座机'
            }
          ],
        },
        fileNamefileList: [],
        templateList: [],
        simpleDeptList: [],
        adoptList: [],
        fwhList: [],
        sendDeptList: [],
        deptArray: [],
        spfileList: [],
      }
    },
    computed: {
      ...mapGetters(["userinfo"]),
      type_id() {
        let path = this.$route.fullPath.split("/")
        let key = path[path.length - 1]
        return key == "schoolDocuments" ? 3 : 4
      },
      mainAction() {
        return process.env.VUE_APP_BASE_API + "/api/document/upload_file/";
      },
    },
    watch: {},
    created() {
      this.formData.type_id = this.type_id
      this.getInfo()
      this.getDocumentAdopt()
    },
    mounted() {
      eventBus.$on("recipient", (recipient) => {
        this.formData.recipient = recipient
      })
    },
    methods: {
      uploadProgress() {
        eventBus.$emit("wordloading")
      },
      checkMyPhone(rule, value, callback) {
        if (value === '') {
          callback(new Error('联系电话不可为空！'));
        } else {
          let regPone = /(^1\d{10}$|^(0\d{2,3}-?|0\d2,3)?[1-9]\d{4,7}(-\d{1,8})?$)/
          if (!regPone.test(value)) {
            callback(new Error("请输入正确的电话格式！"))
          }
          callback();
        }
      },
      remoteMethod(query) {
        if (query !== '') {
          this.getDocumentAdopt(query)
        } else {
          this.adoptList = [];
        }
      },
      querySearch(queryString, cb) {
        schoolDocuments.getDocumentCategory({search_value: queryString}).then(res => {
          this.fwhList = res.data.map(item => {
            return {value: item.name}
          })
          cb(this.fwhList);
        })
      },
      handleSelect(item) {
      },
      getDocumentAdopt(value) {
        schoolDocuments.getDocumentAdopt({search_value: value}).then(res => {
          this.adoptList = res.data
        })
      },
      getInfo() {
        if (this.row && this.row.id) {
          for (var o in this.row) {
            this.formData[o] = this.row[o]
          }
        } else {
          this.formData.alias = this.userinfo.alias;
        }
      },
      mainUploadSuccess(res, file) {
        if (res.code == 0) {
          this.formData.text_file = res.data.wopi
          let index = file.name.lastIndexOf(".")
          this.formData.title = file.name.substring(0, index)
          eventBus.$emit("openWord", res.data.wopi)
        } else {
          this.msgError(res.msg)
        }
      },
      submitForm() {
        let self = this
        return new Promise(function (resolve, reject) {
          self.$refs['elForm'].validate(valid => {
            if (!valid) return
            if (!self.formData.text_file) {
              self.msgError("正文不允许为空！");
              return;
            }
            resolve(self.formData)
          })
        });
      },
      resetForm() {
        this.$refs['elForm'].resetFields()
      },
    }
  }

</script>
<style lang="scss" scoped>
</style>
